package com.workcell.workdata.xthings.controller;


import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.Ignore;
import com.workcell.workdata.common.core.util.R;
import com.workcell.workdata.common.excel.annotation.ResponseExcel;
import com.workcell.workdata.xthings.dto.BaseVo;
import com.workcell.workdata.xthings.dto.UserInfo;
import com.workcell.workdata.xthings.entity.BaseTimeClock;
import com.workcell.workdata.xthings.entity.Manufacturer;
import com.workcell.workdata.xthings.service.BaseTimeClockService;
import com.workcell.workdata.xthings.service.ManufacturerService;
import com.workcell.workdata.xthings.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 考勤机型号
 *
 * @author zhangguangrui
 * @since 2023-03-14
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/baseTimeClock" )
@Api(value = "baseTimeClock", tags = "考勤机型号管理")
@Ignore
public class BaseTimeClockController {
    private final BaseTimeClockService baseTimeClockService;
    private final UserInfoService userInfoService;
    private final ManufacturerService manufacturerService;

    /**
     * 新增
     * @param baseTimeClock
     * @return R
     */
    @ApiOperation(value = "新增")
    @PostMapping
    public R save(@RequestBody BaseTimeClock baseTimeClock) {
        Manufacturer manufacturer = manufacturerService.selectByManufacturerCode(baseTimeClock.getDeviceManufacturer());
        Assert.notNull(manufacturer, "厂商查询为空");
        baseTimeClock.setManufacturerShortName(manufacturer.getManufacturerShortName());
        baseTimeClock.setManufacturerFullName(manufacturer.getManufacturerFullName());
        baseTimeClock.setManufacturer(manufacturer.getManufacturerFullName());
		baseTimeClock.setManufacturerCode(manufacturer.getManufacturerCode());
        return R.ok(baseTimeClockService.save(baseTimeClock));
    }

    /**
     * 修改考勤机型号基本信息
     * @param baseTimeClock
     * @return R
     */
    @ApiOperation(value = "修改考勤机型号")
    @PutMapping
    public R updateBaseTvBox(@RequestBody BaseTimeClock baseTimeClock) {
		Manufacturer manufacturer = manufacturerService.selectByManufacturerCode(baseTimeClock.getDeviceManufacturer());
		Assert.notNull(manufacturer, "厂商查询为空");
		baseTimeClock.setManufacturerShortName(manufacturer.getManufacturerShortName());
		baseTimeClock.setManufacturerFullName(manufacturer.getManufacturerFullName());
		baseTimeClock.setManufacturer(manufacturer.getManufacturerFullName());
		baseTimeClock.setManufacturerCode(manufacturer.getManufacturerCode());
        return R.ok(baseTimeClockService.updateBaseTimeClock(baseTimeClock));
    }

    /**
     * 通过id删除
     * @param id
     * @return R
     */
    @ApiOperation(value = "通过id删除")
    @DeleteMapping("/{id}" )
    public R delById(@PathVariable Long id) {
        return R.ok(baseTimeClockService.delById(id));
    }

    /**
     * 条件分页查询
     * @param baseVo
     * @return
     */
    @PostMapping("/timeClockQueryPage")
    @ApiOperation(value = "条件分页查询",notes = "条件分页查询")
    public R timeClockQueryPage(@RequestBody BaseVo baseVo){
        return R.ok(baseTimeClockService.timeClockQueryPage(baseVo));
    }

    /**
     * 通过id查询
     * @param  id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(baseTimeClockService.getById(id));
    }

    /**
     * 修改状态
     * @param baseTimeClock
     * @return R
     */
    @ApiOperation(value = "修改状态", notes = "修改状态")
    @PutMapping("/updateStateById")
    public R updateStateById(@RequestBody BaseTimeClock baseTimeClock) {
        return R.ok(baseTimeClockService.updateStateById(baseTimeClock));
    }



    /**
     * 考勤机系统接入
     * 添加考勤机人员
     */
    @PutMapping("/addtimeClockUser")
    @ApiOperation(value = "添加考勤机人员",notes = "添加考勤机人员")
    private R addtimeClockUser(@RequestBody UserInfo userInfo){

        return R.ok(userInfoService.addtimeClockUser(userInfo));
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public R getBaseTimeClockPage(Page page,BaseTimeClock timeClock){
        return R.ok(baseTimeClockService.page(page, Wrappers.query(timeClock)));
    }

    /**
     * 导出打卡机excel表格
     * @param timeClock 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<BaseTimeClock> export(BaseTimeClock timeClock) {
        return baseTimeClockService.list(Wrappers.query(timeClock));
    }












}
